<?php
namespace app\api\controller;
use app\BaseController;
use app\common\model\StaffManageModel;

class Manage extends BaseController{
    public function list(){
        $list = StaffManageModel::with(['staff','staffDetail'])->where('user_id',$this->request->user_info->user_id)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->order('staff_manage_id','desc')
            ->select();
        return success(['list'=>$list]);
    }
    public function delete(){
        $staff_manage_id = $this->request->param('staff_manage_id/d',0);
        if($staff_manage_id === 0){
            return error('错误操作');
        }
        if(null === $StaffManageModel = StaffManageModel::find($staff_manage_id)){
            return error('不存在');
        }
        $StaffManageModel->is_delete = 1;
        if($StaffManageModel->save()){
            return success('删除成功');
        }else{
            return error('删除失败');
        }
        
    }
    public function bind(){
        $staff_manage_id = $this->request->param('staff_manage_id/d',0);
        if($this->request->user_info->user_id > 0){
            if(strlen($staff_manage_id) === 11){
               $staff_manage_id = StaffManageModel::where(['bind_mobile'=>$staff_manage_id,'is_delete'=>0,'user_id'=>0])->value('staff_manage_id'); 
            }
            
            if(null === $StaffManageModel = StaffManageModel::find($staff_manage_id)){
                return error('绑定码错误');
            }
            if($StaffManageModel->user_id || $StaffManageModel->is_delete){
                return error('已失效');
            }
            $count = StaffManageModel::where([
                'user_id' => $this->request->user_info->user_id,
                'staff_id' => $StaffManageModel->staff_id
                ])->count('staff_manage_id');
            if($count > 0){
                return error('请勿重复绑定');
            }
            $StaffManageModel->user_id = $this->request->user_info->user_id;
            if($StaffManageModel->save()){
                return success('绑定成功');
            }else{
                return error('绑定失败');
            }
        }else{
            return error('请先登录');
        }
    }
}